extends Control



var DialogColumu = preload("res://tool/page/dialog/DialogColumu.tscn")

# 当前正在编辑的剧本数据，添加剧本数据为空
var edit_drama_data = null

onready var search_input = $searchBox/LineEdit 


func _ready():
	state_update()
	
	$DialogForm.connect("add_dialog", self, "_on_add_dialog")

# 刷新页面状态
func state_update():	
	update_list(DataSource.get_data_list(DataSource.dialig))


func update_list(list):
	
	var box = $table/ScrollContainer/VBoxContainer
	
	for child in box.get_children():
		child.queue_free()
		

	for data in list:
		var ui_item = DialogColumu.instance()
		box.add_child(ui_item)
		ui_item.set_data(data)
		
		ui_item.connect("edit", self, "_on_edit")
		ui_item.connect("delete", self, "_on_delete")
	

# 删除对话
func _on_delete(data):
	MainTool.confirm("你是否确认删除对话", self, "on_del_ok", null, data["id"])
	
func on_del_ok(id):
	DataSource.delete_data_by_id(DataSource.dialig, id)
	state_update()
	

func _on_edit(data):
	$DialogForm.set_data(data)
	$DialogForm.visible = true

func _on_addBtn_button_up():
	$DialogForm.set_data(null)
	$DialogForm.visible = true


func _on_add_dialog(data):
	state_update()


func _on_search_button_up():
	var text = search_input.text
	
	if text:
		var list = []
		for map in DataSource.get_data_list(DataSource.dialig):
			if map["name"].find(text) != -1:
				list.append(map)
				
		update_list(list)
	else:
		update_list(DataSource.get_data_list(DataSource.dialig))
